package ICM.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import common.CommandMessage;
import common.Request;
import common.TextMessage;
import javax.swing.JTextPane;

public class ExecutionUI extends JPanel implements ActionListener, Observer {
	
	private ClientUI ui;
	private Client c;
	private SystemEngineerUI SE;
	private Request request = null;
	private String[] stage = null;
	private JTextField Duration;
	private JButton btnBack;
	private JLabel lblExecution;
	private JLabel lblSetStageDuration;
	private JButton btnSave;
	private JCheckBox chckbxSupervisorApproval;
	private JTextPane StageReport;
	private boolean DurationChange = false;
	private boolean StageReportChange = false;
	private JLabel lblNewLabel;
	private JButton btnSave_1;
	private JLabel lblRequestId;
	private JLabel lblDescription;
	private JLabel lblReasons;
	private JLabel lblComments;
	private JTextPane Description;
	private JTextPane Reasons;
	private JTextPane Comments;
	private JTextPane RequestID;
	private JButton btnFinishStage;
	private boolean StageFinished = false;

	public ExecutionUI(final Client c, final ClientUI ui) {
		this.c=c;
		this.ui=ui;
		c.addObserver(this);
		setLayout(null);
		
		// Get request details
		CommandMessage msg = new CommandMessage("Get request data",new Integer[] {Integer.parseInt(SE.info[0])});
		try {
			c.sendToServer(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Get stage details
		CommandMessage msg1 = new CommandMessage("Get stage data",new Integer[] {Integer.parseInt(SE.info[0]),3});
		try {
			c.sendToServer(msg1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
		    Thread.sleep(1000);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		lblExecution = new JLabel("Execution");
		lblExecution.setFont(new Font("Tahoma", Font.PLAIN, 26));
		lblExecution.setBounds(347, 11, 143, 35);
		add(lblExecution);
		
		lblSetStageDuration = new JLabel("Set stage duration:");
		lblSetStageDuration.setBounds(40, 65, 129, 14);
		add(lblSetStageDuration);
		
		Duration = new JTextField();
		Duration.setBounds(179, 62, 104, 20);
		add(Duration);
		Duration.setColumns(10);
		
		btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CommandMessage msg = new CommandMessage("Set stage duration",new Integer[] {Integer.parseInt(SE.info[0]),3,Integer.parseInt(Duration.getText())});
				try {
					c.sendToServer(msg);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
				    Thread.sleep(400);
				} catch(InterruptedException ex) {
				    Thread.currentThread().interrupt();
				}
			if (DurationChange)
				ui.ShowMessageBox("Information", "Duration was changed" ,JOptionPane.INFORMATION_MESSAGE);
			else
				ui.ShowMessageBox("Error", "Can't change duration" ,JOptionPane.ERROR_MESSAGE);
			}
		});
		btnSave.setBounds(115, 90, 89, 23);
		add(btnSave);
		
		chckbxSupervisorApproval = new JCheckBox("Supervisor approval");
		chckbxSupervisorApproval.setEnabled(false);
		chckbxSupervisorApproval.setBounds(289, 61, 143, 23);
		add(chckbxSupervisorApproval);
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try 
				{try {
					ui.SetContent(ClientUI.SystemEngineerWindow_Screen);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnBack.setBounds(41, 472, 89, 23);
		add(btnBack);
		
		lblNewLabel = new JLabel("Stage report:");
		lblNewLabel.setBounds(513, 65, 89, 14);
		add(lblNewLabel);
		
		StageReport = new JTextPane();
		StageReport.setBounds(509, 93, 274, 167);
		StageReport.setText(stage[3]);
		add(StageReport);
		
		btnSave_1 = new JButton("Save");
		btnSave_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CommandMessage msg = new CommandMessage("Set stage report",new String[] {SE.info[0],"3",StageReport.getText()});
				try {
					c.sendToServer(msg);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
				    Thread.sleep(400);
				} catch(InterruptedException ex) {
				    Thread.currentThread().interrupt();
				}
				if (StageReportChange)
					ui.ShowMessageBox("Information", "Stage report was changed" ,JOptionPane.INFORMATION_MESSAGE);
				else
					ui.ShowMessageBox("Error", "Can't change stage report" ,JOptionPane.ERROR_MESSAGE);
				}
		});
		btnSave_1.setBounds(605, 284, 89, 23);
		add(btnSave_1);
		
		lblRequestId = new JLabel("Request ID:");
		lblRequestId.setBounds(40, 132, 90, 14);
		add(lblRequestId);
		
		lblDescription = new JLabel("Description:");
		lblDescription.setBounds(40, 183, 90, 14);
		add(lblDescription);
		
		lblReasons = new JLabel("Reasons:");
		lblReasons.setBounds(39, 288, 90, 14);
		add(lblReasons);
		
		lblComments = new JLabel("Comments:");
		lblComments.setBounds(40, 383, 90, 14);
		add(lblComments);
		
		Description = new JTextPane();
		Description.setText(request.getDescription());
		Description.setBounds(160, 163, 222, 71);
		add(Description);
		
		Reasons = new JTextPane();
		Reasons.setText(request.getReasons());
		Reasons.setBounds(160, 258, 222, 71);
		add(Reasons);
		
		Comments = new JTextPane();
		Comments.setText(request.getComments());
		Comments.setBounds(160, 356, 222, 71);
		add(Comments);
		
		RequestID = new JTextPane();
		RequestID.setText(Integer.toString(request.getIdRequest()));
		RequestID.setBounds(163, 126, 176, 20);
		add(RequestID);
		
		btnFinishStage = new JButton("Finish stage");
		btnFinishStage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CommandMessage msg = new CommandMessage("Finish execution stage",new String[] {SE.info[0]});
				try {
					c.sendToServer(msg);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
				    Thread.sleep(400);
				} catch(InterruptedException ex) {
				    Thread.currentThread().interrupt();
				}
				if (StageFinished)
					ui.ShowMessageBox("Information", "Stage finished" ,JOptionPane.INFORMATION_MESSAGE);
				else
					ui.ShowMessageBox("Error", "Can't finish stage" ,JOptionPane.ERROR_MESSAGE);
				}
		});
		btnFinishStage.setBounds(605, 379, 112, 41);
		add(btnFinishStage);
		
		if (stage!=null)
		{
			if (stage[2].equals("Active"))
			{
				chckbxSupervisorApproval.setSelected(false);
				Duration.setEditable(true);				
			}
			else
			{
				chckbxSupervisorApproval.setSelected(true);
				Duration.setEditable(false);				
			}
		}
	}

	@Override
	public void update(Observable o, Object arg1) {
		// TODO Auto-generated method stub
		String msg = "";
		String cmd = "";
		if (arg1 instanceof TextMessage)
			msg = ((TextMessage)arg1).ToDebugMessage();
		else if (arg1 instanceof CommandMessage) {
			CommandMessage ms=(CommandMessage)arg1;
			if(ms.getCommand().equals("request data"))
					request = (Request) ms.getParams()[1];		
			else if(ms.getCommand().equals("Stage data"))
				stage = (String[]) ms.getParams()[1];	
			else if(ms.getCommand().equals("Stage duration"))
				DurationChange = Boolean.parseBoolean(ms.getParams()[1].toString());	
			else if(ms.getCommand().equals("Stage report"))
				StageReportChange = Boolean.parseBoolean(ms.getParams()[1].toString());	
			else if(ms.getCommand().equals("Stage finished"))
				StageFinished = Boolean.parseBoolean(ms.getParams()[1].toString());	
		}
		else
			msg = arg1.toString();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
